var o, e = require, t = module, n = exports, i = this && this.__extends || (o = function(e, t) {
    return (o = Object.setPrototypeOf || {
        __proto__: []
    } instanceof Array && function(e, t) {
        e.__proto__ = t;
    } || function(e, t) {
        for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
    })(e, t);
}, function(e, t) {
    function i() {
        this.constructor = e;
    }
    o(e, t), e.prototype = null === t ? Object.create(t) : (i.prototype = t.prototype, 
    new i());
}), a = this && this.__decorate || function(e, t, o, i) {
    var s, c = arguments.length, n = c < 3 ? t : null === i ? i = Object.getOwnPropertyDescriptor(t, o) : i;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(e, t, o, i); else for (var r = e.length - 1; 0 <= r; r--) (s = e[r]) && (n = (c < 3 ? s(n) : 3 < c ? s(t, o, n) : s(t, o)) || n);
    return 3 < c && n && Object.defineProperty(t, o, n), n;
};

Object.defineProperty(n, "__esModule", {
    value: !0
});

var r = e("JoystickCommon"), s = e("Player"), c = e("GameManager"), l = e("PlayerModel"), d = cc._decorator, u = d.ccclass, p = d.property, h = function(t) {
    function e() {
        var e = null !== t && t.apply(this, arguments) || this;
        return e.dot = null, e.ring = null, e.player = null, e.joystickType = r.default.JoystickType.FIXED, 
        e.directionType = r.default.DirectionType.ALL, e._stickPos = null, e._touchLocation = null, 
        e._radius = null, e._pressKeyFlags = 0, e;
    }
    var o;
    return i(e, t), (o = e).prototype.onLoad = function() {
        GA.isWeChat() && cc.sys.os == cc.sys.OS_WINDOWS || !cc.sys.isMobile ? (this._initKeyEvent(), 
        this._initMouseEvent()) : (this._radius = this.ring.width / 2, this._initTouchEvent(), 
        this.joystickType, r.default.JoystickType.FOLLOW);
    }, e.prototype._initTouchEvent = function() {
        this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this), this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, this), 
        this.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this), this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, this);
    }, e.prototype._touchStartEvent = function(e) {
        var t, o = cc.v2(this.node.convertToNodeSpaceAR(e.getLocation()));
        this.joystickType === r.default.JoystickType.FIXED ? (this._stickPos = this.ring.getPosition(), 
        t = o.sub(this.ring.getPosition()).mag(), this._radius > t && this.dot.setPosition(o)) : this.joystickType === r.default.JoystickType.FOLLOW && (this._stickPos = o, 
        this.node.opacity = 255, this._touchLocation = e.getLocation(), this.ring.setPosition(o), 
        this.dot.setPosition(o));
    }, e.prototype._touchMoveEvent = function(e) {
        if (this.joystickType === r.default.JoystickType.FOLLOW && this._touchLocation === e.getLocation()) return !1;
        var t, o, i;
        this.player.model.hasBuffer(l.BufferType.月饼) || this.player.model.hasBuffer(l.BufferType.狗头) || (t = (o = cc.v2(this.ring.convertToNodeSpaceAR(e.getLocation()))).mag(), 
        i = this._stickPos.x + o.x, e = this._stickPos.y + o.y, o = cc.v2(i, e).sub(this.ring.getPosition()).normalize(), 
        this._radius > t ? (this.dot.setPosition(cc.v2(i, e)), this.player._speedType = r.default.SpeedType.NORMAL) : (i = this._stickPos.x + o.x * this._radius, 
        e = this._stickPos.y + o.y * this._radius, this.dot.setPosition(cc.v2(i, e)), this.player._speedType = r.default.SpeedType.FAST), 
        this.player.moveDir = o, this.player.model.hasBuffer(l.BufferType.瞄准) ? this.player.attackDir = o : this.player.attackDir = null);
    }, e.prototype._touchEndEvent = function() {
        this.dot.setPosition(this.ring.getPosition()), this.joystickType == r.default.JoystickType.FOLLOW && (this.node.opacity = 0), 
        this.player._speedType = r.default.SpeedType.STOP;
    }, e.prototype._initKeyEvent = function() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this._keyDownEvent, this), 
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this._keyUpEvent, this);
    }, e.prototype._keyDownEvent = function(e) {
        switch (e.keyCode) {
          case cc.macro.KEY.a:
            this._pressKeyFlags |= o.LEFT;
            break;

          case cc.macro.KEY.d:
            this._pressKeyFlags |= o.RIGHT;
            break;

          case cc.macro.KEY.w:
            this._pressKeyFlags |= o.UP;
            break;

          case cc.macro.KEY.s:
            this._pressKeyFlags |= o.DOWN;
        }
        this._updateDirectionNormalized();
    }, e.prototype._keyUpEvent = function(e) {
        switch (e.keyCode) {
          case cc.macro.KEY.a:
            this._pressKeyFlags &= ~o.LEFT;
            break;

          case cc.macro.KEY.d:
            this._pressKeyFlags &= ~o.RIGHT;
            break;

          case cc.macro.KEY.w:
            this._pressKeyFlags &= ~o.UP;
            break;

          case cc.macro.KEY.s:
            this._pressKeyFlags &= ~o.DOWN;
        }
        this._updateDirectionNormalized();
    }, e.prototype._updateDirectionNormalized = function() {
        var e = cc.Vec2.ZERO;
        0 < (this._pressKeyFlags & (o.LEFT | o.RIGHT)) && (e.x = 0 < (this._pressKeyFlags & o.LEFT) ? -1 : 1), 
        0 < (this._pressKeyFlags & (o.UP | o.DOWN)) && (e.y = 0 < (this._pressKeyFlags & o.DOWN) ? -1 : 1), 
        e.equals(cc.Vec2.ZERO) ? this.player._speedType = r.default.SpeedType.STOP : (e.normalizeSelf(), 
        this.player.moveDir = e.normalizeSelf(), this.player._speedType = r.default.SpeedType.FAST);
    }, e.prototype._initMouseEvent = function() {
        this.node.on(cc.Node.EventType.MOUSE_MOVE, this._mouseMoveEvent, this), this.node.on(cc.Node.EventType.MOUSE_DOWN, this._mouseDownEvent, this), 
        this.node.on(cc.Node.EventType.MOUSE_UP, this._mouseUpEvent, this);
    }, e.prototype._mouseMoveEvent = function(e) {
        var t = cc.v2(this.player.node.convertToWorldSpaceAR(cc.Vec2.ZERO)).sub(cc.v2(c.default.getInstance().gameView.followCamera.node.convertToWorldSpaceAR(cc.Vec2.ZERO))).mulSelf(c.default.getInstance().gameView.followCamera.zoomRatio);
        this.player.attackDir = cc.v2(this.node.convertToNodeSpaceAR(e.getLocation())).sub(t).normalizeSelf();
    }, e.prototype._mouseDownEvent = function(e) {
        c.default.getInstance().gameView.attackView.touchStartHandler(e);
    }, e.prototype._mouseUpEvent = function(e) {
        c.default.getInstance().gameView.attackView.touchEndHandler(e);
    }, e.LEFT = 1, e.RIGHT = 2, e.UP = 4, e.DOWN = 8, a([ p({
        type: cc.Node,
        displayName: "Dot",
        tooltip: "摇杆操纵点"
    }) ], e.prototype, "dot", void 0), a([ p({
        type: cc.Node,
        displayName: "Ring",
        tooltip: "摇杆背景节点"
    }) ], e.prototype, "ring", void 0), a([ p({
        type: s.default,
        displayName: "Player",
        tooltip: "操控角色"
    }) ], e.prototype, "player", void 0), a([ p({
        type: r.default.JoystickType,
        displayName: "Touch Type",
        tooltip: "触摸类型"
    }) ], e.prototype, "joystickType", void 0), a([ p({
        type: r.default.DirectionType,
        displayName: "Direction Type",
        tooltip: "方向类型"
    }) ], e.prototype, "directionType", void 0), o = a([ u ], e);
}(cc.Component);

n.default = h;